//
//  BindAccountsViewController.m
//  Logistics
//
//  Created by hy002 on 2018/1/6.
//  Copyright © 2018年 HUYU. All rights reserved.
//

#import "BindAccountsViewController.h"

static int seconds = 60;//计时60s
@interface BindAccountsViewController ()<UITextFieldDelegate>
{
    UITextField *phoneField;
    UITextField *codeField;
    UIButton *codeBtn;
    NSTimer *timer;
}
@end

@implementation BindAccountsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
}

- (void)creatUI
{
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BacksColor;
    self.navigationItem.title = @"体验馆";
    NSArray *imgAry = @[@"my_icon04",@"my_icon06"];
    NSArray *pladerAry = @[@"手机号",@"验证码"];
    for (int i = 0; i < pladerAry.count; i++)
    {
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, 30*displayheight+i*55*displayheight, SCREEN_WIDTH-30*displaywidth, 45*displayheight)];
        backView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:backView];
        UIImageView *imageview = [ZCControl createImageViewWithFrame:CGRectMake(5*displaywidth, 12.5*displayheight, 20*displaywidth, 20*displayheight) ImageName:imgAry[i]];
        imageview.contentMode = UIViewContentModeScaleAspectFit;
        [backView addSubview:imageview];
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(35*displaywidth, 12.5*displayheight, 1, 20*displayheight)];
        line.backgroundColor = RGB(204, 204, 204);
        [backView addSubview:line];
        UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(40*displaywidth, 2.5*displayheight, SCREEN_WIDTH-70*displaywidth, 40*displayheight) placeholder:pladerAry[i] passWord:NO leftImageView:nil rightImageView:nil Font:15.0];
        [textfield setValue:RGB(135, 135, 135) forKeyPath:@"_placeholderLabel.textColor"];
        textfield.delegate = self;
        textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
        textfield.textColor = RGB(48, 48, 48);
        textfield.textAlignment = NSTextAlignmentLeft;
        [backView addSubview:textfield];
        if (i == 0)
        {
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            phoneField = textfield;
        }
        else
        {
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            textfield.frame = CGRectMake(40*displaywidth, 2.5*displayheight, SCREEN_WIDTH-170*displaywidth, 40*displayheight);
            codeField = textfield;
            codeBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-130*displayheight, 10*displayheight, 90*displayheight, 25*displayheight) ImageName:nil Target:self Action:@selector(verifiBtnAction:) Title:@"获取验证码" Font:12.0];
            [codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            codeBtn.backgroundColor = RGB(255, 153, 20);
            [codeBtn.layer setCornerRadius:4];
            [codeBtn.layer setMasksToBounds:YES];
            [backView addSubview:codeBtn];
        }
    }
    UIButton *submitBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, 160*displayheight, SCREEN_WIDTH-30*displaywidth, 45*displayheight) ImageName:nil Target:self Action:@selector(submitAction) Title:@"提交" Font:18.0];
    submitBtn.backgroundColor = RGB(255, 153, 20);
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitBtn.layer.cornerRadius = 5;
    submitBtn.layer.masksToBounds = YES;
    [self.view addSubview:submitBtn];
}

- (void)verifiBtnAction:(UIButton *)btn
{
    if (phoneField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入手机号" addToView:self.view];
        return;
    }
    if (phoneField.text.length != 11)
    {
        [LTools showMBProgressWithText:@"请输入正确的手机号" addToView:self.view];
        return;
    }
    [self requestRegisCode4register];
    
}

//注册验证码
- (void)requestRegisCode4register
{
    [self.view endEditing:YES];
    WEAKSELF;
    NSDictionary *dic = @{@"phone":phoneField.text};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestWithTheFormUrl:SERVER_BindAccount Parmers:dic Success:^(id obj) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        if ([obj server_Getcodecheck])
        {
            [LTools showMBProgressWithText:@"验证码已发送" addToView:weakSelf.view];
            [weakSelf startTimer];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];

    }];
    
}

- (void)startTimer
{
    seconds = 60;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(calculateTime) userInfo:Nil repeats:YES];
    codeBtn.userInteractionEnabled = NO;
}
//计算时间
- (void)calculateTime
{
    NSString *title = [NSString stringWithFormat:@"(%dS)重新获取",seconds];
    codeBtn.titleLabel.text = title;
    [codeBtn setTitle:title forState:UIControlStateNormal];
    
    if (seconds != 0) {
        seconds --;
    }else
    {
        [self renewTimer];
    }
    
}
//计时器归零
- (void)renewTimer
{
    [timer invalidate];//计时器停止
    codeBtn.userInteractionEnabled = YES;
    [codeBtn setTitle:@"重新获取验证码" forState:UIControlStateNormal];
    seconds = 60;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
    [self.view endEditing:YES];
}


//提交
- (void)submitAction
{
    if (phoneField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入手机号" addToView:self.view];
        return;
    }
    if (phoneField.text.length != 11)
    {
        [LTools showMBProgressWithText:@"请输入正确的手机号" addToView:self.view];
        return;
    }
    if ([codeField.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请输入验证码" addToView:self.view];
        return;
    }
    [self.view endEditing:YES];
    WEAKSELF;
    NSDictionary *dic = @{@"phone":phoneField.text,
                          @"code":codeField.text};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestWithTheFormUrl:SERVER_Bindrelevance Parmers:dic Success:^(id obj) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        if ([obj server_Getcodecheck])
        {
            [LTools showMBProgressWithText:@"绑定成功" addToView:weakSelf.view];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
